/**
 * @file turn.c
 * @brief
 * @author xuesong (wenshaoliu@foxmail.com)
 * @version 1.0
 * @date 2024-09-11
 *
 * @copyright Copyright (c) 2024  by xuesong
 *
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2024-09-11     <td>1.0     <td>xuesong   <td>内容
 * </table>
 */
#include "turn.h"

uint16_t turnCnt = 0;
TurnStepStruct xTurnFifo;

/* 中断复用函数,输入中断类型即可 */
void prvTurnProcessISR(IntTrigDef xIntType)
{
    uint8_t CurrentCnt = (sizeof(TurnStepFlag_t) << 3) - 1;
    TurnStepFlag_t flagBit = (0x01 << CurrentCnt);
    static IntTrigDef lastTrigType = INT_A_DN;
    TurnStepAction_t BackAction = 0;
    uint8_t FlagCnt = 0;
    for (; flagBit; flagBit >>= 1)
    {
        if (flagBit & xTurnFifo.flag)
        {
            CurrentCnt--;
            continue;
        }
        xTurnFifo.flag |= flagBit;
        xTurnFifo.actionFifo.Action &= (~((INT_TRIG_MASK) << (CurrentCnt * INT_TRIG_MASK_BITS)));
        xTurnFifo.actionFifo.Action |= ((xIntType & INT_TRIG_MASK) << (CurrentCnt * INT_TRIG_MASK_BITS));
        break;
    }
    // if ((xTurnFifo.flag & 0xC0) == 0xC0)
    {
        // lastTrigType = ((xTurnFifo.actionFifo.Action >> ((CurrentCnt + 1) * 3)) & INT_TRIG_MASK);
        // if ((xIntType == INT_A_DN && lastTrigType == INT_A_UP) || (xIntType == INT_B_DN && lastTrigType == INT_B_UP)) // 过滤无效上升沿
        if ((xIntType == INT_BH_ADN && lastTrigType == INT_BH_ADP) || (xIntType == INT_AH_BDN && lastTrigType == INT_AH_BDP)) // 过滤无效上升沿
        {
            xTurnFifo.flag ^= flagBit;
            xTurnFifo.flag ^= (flagBit << 1);
        }
        lastTrigType = xIntType;
    }
    if ((xTurnFifo.flag & 0xF8) == 0xF8)
    {
        BackAction = xTurnFifo.actionFifo.Action >> (INT_TRIG_MASK_BITS * 4);
        IntTrigDef debugTrigType = BackAction & INT_TRIG_MASK;
        FlagCnt = debugTrigType & 0x03;
        debugTrigType = (BackAction >>= INT_TRIG_MASK_BITS) & INT_TRIG_MASK;
        FlagCnt += (debugTrigType & 0x03);
        debugTrigType = (BackAction >>= INT_TRIG_MASK_BITS) & INT_TRIG_MASK;
        FlagCnt += (debugTrigType & 0x03);
        debugTrigType = (BackAction >>= INT_TRIG_MASK_BITS) & INT_TRIG_MASK;
        FlagCnt += (debugTrigType & 0x03);
        // FlagCnt = (BackAction & 0x03);
        // FlagCnt += ((BackAction >>= INT_TRIG_MASK_BITS) & 0x03);
        // FlagCnt += ((BackAction >>= INT_TRIG_MASK_BITS) & 0x03);
        // FlagCnt += ((BackAction >>= INT_TRIG_MASK_BITS) & 0x03);
        if (0x06 == FlagCnt) // 连续4个动作包含A B两个下降波形则计数
        {
            turnCnt++;
            BackAction = 4; // 移除记录
        }
        else
        {
            BackAction = 1; // 移除一个无效记录，理论上应该发生了一次反转
        }
        xTurnFifo.flag <<= BackAction;
        xTurnFifo.actionFifo.Action <<= (BackAction * INT_TRIG_MASK_BITS);
    }
}

uint8_t jumpAction[] = {
    6,
    0,
    3,
    2,
    1,
    0,
    3,
    2,
    1,
    0,
    3,
    2,
    1,
    0,
    3,
    2,
    1,
    7,
    6,
    0,
    3,
    2,
    1,
    0,
    3,
    2,
    1,
    7,
    6,
    0,
    3,
    2,
    1,
    0,
    3,
    2,
    1,
    7,
    6,
    0,
    3,
    2,
    1,
    0,
    3,
    5,
    4,
    2,
    1,
    0,
    3,
    5,
    6,
    7,
    6,
    0,
    3,
    5,
    6,
    0,
    3,
    2,
    1,
    0,
    3,
    5,
    6,
    0,
    3,
    5,
    6,
    0,
    3,
    5,
    6,
    0,
    3,
    5,
    6,
    0,
    3,
    5,
    6,
    0,
    3,
    5,
    6,
    0,
    3,
    5,
    6,
    0,
    3,
    5,
    6,
    0,
    3,
    2,
    1,
    0,
    3,
    3,
};

#include "stdio.h"
void jumpActionCnt(void)
{
    for (uint8_t i = 0; i < sizeof(jumpAction); i++)
        prvTurnProcessISR(jumpAction[i]);
    printf("jump cnt = %d\r\n", turnCnt);
}
